package service;

import dao.UserDao;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import relationEntity.MemberUser;
import relationEntity.UserMember;
import util.Page;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by jifeng on 2016/12/2 0002.
 */
@Transactional
public class UserService {
    @Autowired
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public User addUser(User user){
        userDao.addUser(user);
        return user;
    }

    public void deleteUser(int id){
        userDao.deleteUser(id);
    }
    public void updateUser(int id){
        userDao.updateUser(id);
    }
    
    public void updateUser(User user){
        userDao.updateUserPwd(user);;
    }

    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */
    public Page findUserList(int currentPage,String date,String source,String level,String userName, int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = userDao.getUserAllRowCount(date,source,level,userName);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<UserMember> list = userDao.findUserList(offset,date,source,level,userName,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }

    public void  deleteUsers(String ids){
        userDao.deleteUsers(ids);
    }

    public void  recycleUser(String ids){
        userDao.recycleUser(ids);
    }

    public void deleteIntegral(String ids){
        userDao.deleteIntegral(ids);
    }
    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */
    public Page recycleUserList(int currentPage,String date,String source,String level,String userName, int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = userDao.getRecycleAllRowCount(date,source,level,userName);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<UserMember> list = userDao.recycleUserList(offset,date,source,level,userName,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }

    /**
     * 分页查询
     * @param currentPage 当前页号：现在显示的页数
     * @param pageSize 每页显示的记录条数
     * @return 封闭了分页信息(包括记录集list)的Bean
     * */
    public Page IntegralManagementList(int currentPage,String date,String integral,String level,String userName,int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = userDao.getIntegralAllRowCount(date,integral,level,userName);
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<MemberUser> list = userDao.IntegralManagementList(offset,date,integral,level,userName,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }


    public UserMember findUserMemberById(int id){
        return userDao.findUserMemberById(id);
    }

    public void updateUserM(int id,String userName,String name,String gender,String phone,String email){
        userDao.updateUserM(id,userName,name,gender,phone,email);
    }

    public List<Personnel> findPersonnel(String userName,String password){
        return userDao.findPersonnel(userName,password);
    }

    public List<Personnel> findPersonnelByName(String userName){
        return userDao.findPersonnelByName(userName);
    }

    public Role findRole(int id){
        return userDao.findRole(id);
    }

    public List<Menu> menuList(String ids){
        return userDao.menuList(ids);
    }

    public void updateRole(int id,String productListIds,String userListIds,String orderListIds,String systemListIds){
        userDao.updateRole(id,productListIds,userListIds,orderListIds,systemListIds);
    }

    public List<Role> findRoleList(){
        return userDao.findRoleList();
    }

    public Page findRoleList(int currentPage, int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = userDao.getRoleAllRowCount();
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Role> list = userDao.findRoleList(offset,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }

    public  void updatePersonnel(Personnel personnel,String password){
        userDao.updatePersonnel(personnel,password);
    }

    public Page findPersonnelList(int currentPage, int pageSize){
        Page page = new Page();
        //总记录数
        int allRow = userDao.getPersonnelAllRowCount();
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Personnel> list = userDao.findPersonnelList(offset,pageSize);
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        return page;
    }

    public void addRole(Role role){
        userDao.addRole(role);
    }

    public void addPersonnel(Personnel personnel){
        userDao.addPersonnel(personnel);
    }

    public void deletePersonnel(int id){ userDao.deletePersonnel(id);}

    public List<Personnel> findPersonnelListById(int id){ return userDao.findPersonnelListById(id);}

    public void deleteRole(int id){ userDao.deleteRole(id);}

    public List<Advertisement> findAdvertisementList(){
        return userDao.findAdvertisementList();
    }

    public void addAdvertisement(Advertisement advertisement){
        userDao.addAdvertisement(advertisement);
    }

    public void updateAdvertisement(int id,Advertisement advertisement){
        userDao.updateAdvertisement(id,advertisement);
    }

    public List<Advertisement> findAdById(int id){
        return userDao.findAdById(id);
    }

    public void deleteAdvertisement(int id){
        userDao.deleteAdvertisement(id);
    }

    public List<User> findUserByPhone(String phone){
        return userDao.findUserByPhone(phone);
    }

    public void updateDefault(int userId,int addressId){
        userDao.updateDefault(userId,addressId);
    }

    public User findUserById(int id){
        return userDao.findUserById(id);
    }
    public  List<User> findUserByPhonePd(String phone,String password){
        return userDao.findUserByPhonePd(phone,password);
    }

    public void deleteAddresses(int id){
        userDao.deleteAddresses(id);
    }

    public void updateUser(String path,String name,String sex,int id){userDao.updateUser(path,name,sex,id);}

}
